﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RegistrationController;
using InPatientController;

namespace HPMDesktop.Patient
{
    public partial class frmPatientDetails : Form
    {
        private PatientRegistration _objPatient;
        private bool _tabProcedureVisited;
        private bool _tabConsultantVisitsVisited;
        private bool _tabWardVisited;
        
        public frmPatientDetails(PatientRegistration objPatient)
        {
            InitializeComponent();
            _objPatient = objPatient;
            _tabProcedureVisited = false;
            _tabConsultantVisitsVisited = false;
            _tabWardVisited = false;
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmPatientDetails_Load(object sender, EventArgs e)
        {
            // Show or hide the Doctor Visit tab and Bed / ward tab based on patient type
            if (_objPatient.PatientType.Equals("Out-Patient"))
            {
                tabControl1.TabPages.RemoveAt(4);
                tabControl1.TabPages.RemoveAt(2);
                cmdChngPType.Visible = true;
            }

            g_txtpname.Text = _objPatient.PatientInfo.FName + " " +
                            _objPatient.PatientInfo.MName + " " +
                            _objPatient.PatientInfo.LName;
            g_txtCRNum.Text = _objPatient.CRNumber;
            g_txtConsultant.Text = _objPatient.Consultant;

            txtPID.Text = _objPatient.PatientInfo.PatientID;
            txtCRN.Text = _objPatient.CRNumber;

            txtpFname.Text = _objPatient.PatientInfo.FName;
            txtpMname.Text = _objPatient.PatientInfo.MName;
            txtpLname.Text = _objPatient.PatientInfo.LName;
            
            txtFatherFname.Text = _objPatient.PatientInfo.FatherFName;
            txtFatherMname.Text = _objPatient.PatientInfo.FatherMName;
            txtFatherLname.Text = _objPatient.PatientInfo.FatherLName;

            txtStreet.Text = _objPatient.PatientInfo.Address1;
            txtCity.Text = _objPatient.PatientInfo.City;
            txtState.Text = _objPatient.PatientInfo.State;
            txtPhone.Text = _objPatient.PatientInfo.MobilePhone;

            txtCons.Text = _objPatient.Consultant;
            chkPatientType.Text = _objPatient.PatientType;
            if (_objPatient.PatientInfo.Sex.Equals("F"))
            {
                rdoFemale.Checked = true;
                rdoMale.Checked = false;
            } else
            {
                rdoMale.Checked = true;
                rdoFemale.Checked = false;
            }
            txtAge.Text = _objPatient.PatientInfo.Age;
        }

        private void cmdChngPType_Click(object sender, EventArgs e)
        {
            frmShiftOutPatient frm = new frmShiftOutPatient(this, g_txtpname.Text, txtCRN.Text, txtCons.Text);
            frm.Show();
        }

        public void shift_outpatient_to_inpatient(string consultant)
        {
            txtCons.Text = consultant;
            chkPatientType.Text = "In-Patient";
            g_txtConsultant.Text = consultant;
            tabControl1.TabPages.Insert(2, tabDoctorVisit);
            tabControl1.TabPages.Insert(4, tabWardDet);
            cmdChngPType.Visible = false;
        }

        public void setPackageDetails(string packageName, string packageAmount)
        {
            _objPatient.PackageName = packageName;
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedTab == tabProcDet)
            {
                if (!_tabProcedureVisited)
                {
                    if (_objPatient.PackageName == null ||
                        _objPatient.PackageName.Equals(""))
                    {
                        /* get package name */
                        PatientDetails_DataLoader objDataLoader = new PatientDetails_DataLoader();
                        objDataLoader.Get_Package_Details(_objPatient.CRNumber, _objPatient);
                        objDataLoader.Dispose();
                    }

                    txtPackage.Text = _objPatient.PackageName;

                    setupProcedureView();
                    updateProcedureView();

                    _tabProcedureVisited = true;
                }
            }
            else if (tabControl1.SelectedTab == tabDoctorVisit)
            {
                if (!_tabConsultantVisitsVisited)
                {
                    setupConsultantVisitsView();
                    updateConsultantVisitsView();

                    _tabConsultantVisitsVisited = true;
                }
            }
            else if (tabControl1.SelectedTab == tabWardDet)
            {
                if (!_tabWardVisited)
                {
                    setupWardBedHistoryView();
                    updateWardBedHistoryView();

                    _tabWardVisited = true;
                }
            }
        }

        private void cmdAddProc_Click(object sender, EventArgs e)
        {
            ProcedureDetails objProcedure = new ProcedureDetails();
            objProcedure.CRNumber = g_txtCRNum.Text;
            objProcedure.Consultant = g_txtConsultant.Text;
            frmAddUpdateProcedure frm = new frmAddUpdateProcedure(true, this, "frmPatientDetails",
                                                                  g_txtpname.Text, objProcedure);
            frm.Show();
        }

        private void cmdUpdProc_Click(object sender, EventArgs e)
        {
            ProcedureDetails objProcedure = new ProcedureDetails();
            objProcedure.CRNumber = txtCRN.Text;
            if (dataGridProcedures.SelectedRows.Count > 0 &&
                dataGridProcedures.SelectedRows[0].Cells["ID"].Value != null &&
                dataGridProcedures.SelectedRows[0].Cells["ID"].Value.ToString() != "")
            {
                objProcedure.ID = dataGridProcedures.SelectedRows[0].Cells["ID"].Value.ToString();
                objProcedure.ProcedureName = dataGridProcedures.SelectedRows[0].Cells["ProcedureName"].Value.ToString();
                objProcedure.ProcedureDate = dataGridProcedures.SelectedRows[0].Cells["Date"].Value.ToString();
                objProcedure.Consultant = dataGridProcedures.SelectedRows[0].Cells["ConsultantName"].Value.ToString();
                objProcedure.Paid = (bool)dataGridProcedures.SelectedRows[0].Cells["Paid"].Value;
                objProcedure.ReceiptID = dataGridProcedures.SelectedRows[0].Cells["ReceiptID"].Value.ToString();
                objProcedure.Remarks = dataGridProcedures.SelectedRows[0].Cells["Remarks"].Value.ToString();

                frmAddUpdateProcedure frm = new frmAddUpdateProcedure(false, this, "frmPatientDetails",
                                                                      g_txtpname.Text, objProcedure);
                frm.Show();
            }
            else
            {
                MessageBox.Show("Please select a procedure");
            }
        }

        private void setupProcedureView()
        {
            dataGridProcedures.AutoSize = true;
            dataGridProcedures.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
            dataGridProcedures.MultiSelect = false;
            dataGridProcedures.ScrollBars = ScrollBars.Both;
            dataGridProcedures.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridProcedures.AllowUserToOrderColumns = true;
            dataGridProcedures.AllowUserToResizeColumns = true;
            dataGridProcedures.Enabled = true;
            dataGridProcedures.EditMode = DataGridViewEditMode.EditProgrammatically;
        }

        private void setupConsultantVisitsView()
        {
            dataGridDocVisits.AutoSize = true;
            dataGridDocVisits.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
            dataGridDocVisits.MultiSelect = false;
            dataGridDocVisits.ScrollBars = ScrollBars.Both;
            dataGridDocVisits.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridDocVisits.AllowUserToOrderColumns = true;
            dataGridDocVisits.AllowUserToResizeColumns = true;
            dataGridDocVisits.Enabled = true;
            dataGridDocVisits.EditMode = DataGridViewEditMode.EditProgrammatically;
        }

        private void setupWardBedHistoryView()
        {
            dataGridWardHistory.AutoSize = true;
            dataGridWardHistory.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
            dataGridWardHistory.MultiSelect = false;
            dataGridWardHistory.ScrollBars = ScrollBars.Both;
            dataGridWardHistory.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridWardHistory.AllowUserToOrderColumns = false;
            dataGridWardHistory.AllowUserToResizeColumns = true;
            dataGridWardHistory.EditMode = DataGridViewEditMode.EditProgrammatically;
        }

        public void updateProcedureView()
        {
            // clear the grids data source
            dataGridProcedures.DataSource = null;

            Procedure_DataLoader objProcDataLoader = new Procedure_DataLoader();
            DataSet ds = objProcDataLoader.Get_Patient_Procedure_Data(g_txtCRNum.Text);

            if (ds != null)
            {
                dataGridProcedures.DataSource = ds.Tables[0];
                dataGridProcedures.Columns["ID"].Visible = false;
                dataGridProcedures.Columns["ProcedureName"].HeaderText = "Procedure";
                dataGridProcedures.Columns["ConsultantName"].HeaderText = "Consultant";
                dataGridProcedures.Columns["ReceiptID"].HeaderText = "Receipt ID";
                dataGridProcedures.Sort(dataGridProcedures.Columns["ID"], ListSortDirection.Descending);
            }
        }

        public void updateConsultantVisitsView()
        {
            // clear the grids data source
            dataGridDocVisits.DataSource = null;

            PatientDetails_DataLoader objDataLoader = new PatientDetails_DataLoader();
            DataSet ds = objDataLoader.Get_Consultant_Visits(g_txtCRNum.Text);

            if (ds != null)
            {
                dataGridDocVisits.DataSource = ds.Tables[0];
                dataGridDocVisits.Columns["ID"].Visible = false;
                dataGridDocVisits.Columns["ConsultantName"].HeaderText = "Consultant";
                dataGridDocVisits.Columns["Date"].HeaderText = "Visit Date/Time";
                dataGridDocVisits.Sort(dataGridDocVisits.Columns["ID"], ListSortDirection.Ascending);
            }
        }

        public void updateWardBedHistoryView()
        {
            // clear the grids data source
            dataGridWardHistory.DataSource = null;

            PatientWard_DataLoader objDataLoader = new PatientWard_DataLoader();
            DataSet ds = objDataLoader.Get_PatientWard_History(g_txtCRNum.Text);

            if (ds != null)
            {
                dataGridWardHistory.DataSource = ds.Tables[0];
                dataGridWardHistory.Columns["ID"].Visible = false;
                dataGridWardHistory.Columns["WardName"].HeaderText = "Ward";
                dataGridWardHistory.Columns["WardName"].SortMode = DataGridViewColumnSortMode.NotSortable;
                dataGridWardHistory.Columns["BedNumber"].HeaderText = "Bed";
                dataGridWardHistory.Columns["BedNumber"].SortMode = DataGridViewColumnSortMode.NotSortable;
                dataGridWardHistory.Columns["StartDate"].HeaderText = "Start Date/Time";
                dataGridWardHistory.Columns["StartDate"].SortMode = DataGridViewColumnSortMode.NotSortable;
                dataGridWardHistory.Columns["EndDate"].HeaderText = "End Date/Time";
                dataGridWardHistory.Columns["EndDate"].SortMode = DataGridViewColumnSortMode.NotSortable;
                dataGridWardHistory.Sort(dataGridWardHistory.Columns["ID"], ListSortDirection.Descending);

                if (ds.Tables[0].Rows.Count > 0 &&
                    dataGridWardHistory.Rows[0].Cells["EndDate"].Value.ToString().Equals(""))
                {
                    txtCurrentWard.Text = dataGridWardHistory.Rows[0].Cells["WardName"].Value.ToString() + ", " + dataGridWardHistory.Rows[0].Cells["BedNumber"].Value.ToString();
                    wardDischargeEnable(true);
                }
                else
                {
                    wardDischargeEnable(false);
                }
            }
        }

        private void cmdOk_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmdAddDocVisit_Click(object sender, EventArgs e)
        {
            frmAddConsultantVisit frm = new frmAddConsultantVisit(this, "frmPatientDetails",
                                                                  g_txtpname.Text, g_txtCRNum.Text);
            frm.Show();
        }

        private void cmdWardAlloc_Click(object sender, EventArgs e)
        {
            frmWardAllocation frm = new frmWardAllocation(this, "frmPatientDetails",
                                                          g_txtpname.Text, g_txtCRNum.Text);
            frm.Show();
        }

        private void cmdDischarge_Click(object sender, EventArgs e)
        {
            /* Discharge from current ward / bed */

            DateTime dischargeDate = new DateTime(dtDischargeDate.Value.Year,
                                                  dtDischargeDate.Value.Month,
                                                  dtDischargeDate.Value.Day,
                                                  tmDischargeTime.Value.Hour,
                                                  tmDischargeTime.Value.Minute,
                                                  tmDischargeTime.Value.Second);

            PatientWard_Mapper objMapper = new PatientWard_Mapper();
            if (objMapper.Discharge_Patient_Ward(g_txtCRNum.Text, dischargeDate))
            {
                txtCurrentWard.Text = "";
                dataGridWardHistory.Rows[0].Cells["EndDate"].Value = dischargeDate.ToString();
                wardDischargeEnable(false);
            }
            else
            {
                MessageBox.Show("Error in discharging patient. Try again later.");
            }
        }

        private void wardDischargeEnable(bool enable)
        {
            lblDischargeDate.Visible = enable;
            dtDischargeDate.Visible = enable;
            tmDischargeTime.Visible = enable;
            cmdDischarge.Visible = enable;
        }
    }
}
